#include <stdio.h>
#include "matrix.h"
#include "data.h"
#include "image.h"
#include "connected_layer.h"
#include "softmax_layer.h"
#include "input_layer.h"
#include "network.h"
#include "minst_data_set.h"

void add_connected_layer(network *net)
{
    int batch,inputs, outputs;
    int batch_normalize = 0;
    int adam=0;
    char activation_s[]="relu";
    ACTIVATION activation = get_activation(activation_s);
    outputs = net->truths;
    inputs = net->inputs;
    batch=net->batch;
    
    layer l = make_connected_layer(batch, inputs, outputs, activation, batch_normalize, adam);

    // int i,j;
    printf("weight:\n");
    for (int i = 0; i < outputs; i++) {
        for (int j = 0; j < inputs; j++) {
            // l.weights[i * inputs + j] = 0.1 * (i + 1);
            l.weights[i * inputs + j] = 0.01;
            // printf("%f ", l.weights[i * inputs + j]);
        }
        // printf("\n");
    }
    net->layers[0] = l;
}

void add_softmax_layer(network *net)
{
    layer l = make_softmax_layer(net->batch, net->layers[0].outputs, 1);
    net->layers[1] = l;
}
int main()
{
    int ret = 0;
    network *net;
    char data_set[] = "./train-images.idx3-ubyte";
    char data_label[] = "./train-labels.idx1-ubyte";
    int i,j,k;
    data d = {0};

    ret = create_minst_data_set(data_set, data_label);

    net = make_network(2);
    net->batch = 6;
    d = batch_load_minst_data(net->batch, 0, 0);
    net->inputs = d.X.cols;
    net->truths = d.y.cols;
    net->input = calloc(net->inputs * net->batch, sizeof(float));
    net->truth = calloc(net->truths * net->batch, sizeof(float));
    
    add_connected_layer(net);
    add_softmax_layer(net);
    layer l = net->layers[net->n - 1];
    get_next_batch(d, net->batch, 0 * net->batch, net->input, net->truth);
    // for (i = 0; i < 100; i++) {
        // forward_network(net);
    // }
    train_network_datum(net);

    printf("output:\n");
    for (j = 0; j < net->batch;j++) {
        for (i = 0; i < l.outputs; i++) {
            printf("%f ", l.output[j * l.outputs + i]);
        }
        printf("\n");
    }

    destroy_minst_data_set();

    return ret;
}